package com.example.caimogu.businessapp;


import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.caimogu.fragment.HistoryOrder;
import com.example.caimogu.fragment.MyStore;
import com.example.caimogu.fragment.UnhandledOrder;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rgs;
    private RadioButton rb;
    private String onlyBid;


    //Fragment Object
    private FragmentManager fManager;
    private FragmentTransaction fTransaction = null;
    public String curFragmentTag = "";

    private UnhandledOrder unhandledOrder;
    private HistoryOrder historyOrder;
    private MyStore myStore;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragment();

        rgs = (RadioGroup) findViewById(R.id.tabs_rg);
        rgs.setOnCheckedChangeListener(this);

        rb = (RadioButton)findViewById(R.id.tab_rb_unhandledorder);
        rb.setChecked(true);

    }

    private void initFragment() {
        fManager = getFragmentManager();
        unhandledOrder = new UnhandledOrder(this,getLayoutInflater());

        historyOrder = new HistoryOrder(this,getLayoutInflater());
        myStore = new MyStore(this,getLayoutInflater());


        fTransaction = fManager.beginTransaction();
        fTransaction.add(R.id.tab_content,unhandledOrder);
        fTransaction.add(R.id.tab_content, historyOrder);
        fTransaction.add(R.id.tab_content, myStore);
        hideAllFragment(fTransaction);
        fTransaction.show(unhandledOrder);
        fTransaction.commit();

        Intent intent = getIntent();
        onlyBid = intent.getStringExtra("onlyBid");
        Toast.makeText(this,onlyBid,Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.tab_rb_unhandledorder:
                    fTransaction.show(unhandledOrder);
                break;
            case R.id.tab_rb_history:
                    fTransaction.show(historyOrder);
                break;
            case R.id.tab_rb_mystore:
                    fTransaction.show(myStore);
                break;
        }
        fTransaction.commit();
    }

    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(unhandledOrder != null)fragmentTransaction.hide(unhandledOrder);
        if(historyOrder != null)fragmentTransaction.hide(historyOrder);
        if(myStore != null)fragmentTransaction.hide(myStore);

    }

    public String getOnlyBid(){
        return onlyBid;
    }

}
